﻿
get_Vehicle_information()
get_Vehicle_type()
$(document).on("change", "#Fm_cydw_1", function () {
    get_Vehicle_information()
    get_Vehicle_type()
    $("#cx_id").text("")
    $("#jt_tbody tr").remove()
})
$(document).on("click", "#ckxx", function (e) {
    var id = $(e.target).parent().prev().text()
    console.log(id)
    get_Vehicle_information_type(id)
    get_JTJ(id)
    $("#cx_id").text(id)
    $("#jtj").css({"display":"block"})
})

$(document).on("click", "#xj_tjcx", function () {
    var index=1
    var name_ss = $("#CYDW_name").val()
    $("#xj_cxsx tr").each(function (i, item) {
        index++
    })
    var $tr =
        $(`
                    <tr>
                        <th><input type="text" id="xj_cxNAME"/></th>
                        <th><input type="text" value="${name_ss}" class="gsming"/></th>
                        <th><a id="xj_scxj">删除</a></th>
                    </tr>
        `)
    $("#xj_cxsx").append($tr)
})
$(document).on("click", "#xj_scxj", function (e) {
    $(e.target).parent().parent().remove()
})

$(document).on("keydown", "#CYDW_name", function (e) {
    $(".gsming").val($(e.target).val())
})
$(document).on("click", "#XG_quxiao", function (e) {
    var XZ_name = []
    XZ_name.push({
        cy_name: $("#CYDW_name").val()
    })
    console.log(XZ_name)
    $.ajax({
        url: '/Freight/add_cy',
        type: 'GET',
        data: JSON.stringify(XZ_name),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var BZJG = []
            $("xj_cxsx tr").each(function (i, item) {
                BZJG.push({
                    cx_ss: data,
                    cx_name: $("input[id=xj_cxNAME]", item).val()
                })
            })
            
            console.log("共" + BZJG.length + "数据");
            console.log(BZJG);
            $.ajax({
                url: "/Freight/add_cx",
                type: 'POST',
                data: JSON.stringify(BZJG),
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (text) {

                },
                error: function (text) {
                    alert("系统错误" + text);
                }
            })
        },
        error: function () {
            alert('Request Error');
        }
    })
})
//阶梯价增加阶梯位
$(document).on("click", "#zhenjia_jt", function (e) {
    var $tr =
        $(`
                    <tr>
                        <td><input type="number" value="" style="width:60px"/></td>
                        <td style="width:60px">公里以下</td>
                        <td><input type="number" value="" style="width:60px"/></th>
                        <td style="width:60px">元/公里</th>
                        <td style="width:60px" ><a id="shc_jtw">删除阶梯</a></th>
                    </tr>
                `)
    $("#jt_tbody").append($tr)
    $("#JT_zgjt").val("")
    $("#JT_zgj").val("")
})
//对现有承运单位添加车辆信息
$(document).on("click", "#xj_TXxy", function (e) {
    if ($("#xingjian_a1").val() != null) {
        var BZJG = []
        BZJG.push({
            cx_ss: $("#Fm_cydw_1").val(),
            cx_name: $("#xingjian_a1").val()
        })

        console.log("共" + BZJG.length + "数据");
        console.log(BZJG);
        $.ajax({
            url: "/Freight/add_cx",
            type: 'POST',
            data: JSON.stringify(BZJG),
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (text) {
                location.reload(true)
            },
            error: function (text) {
                alert("系统错误" + text);
            }
        })
    }
    
})


//获取车辆信息
function get_Vehicle_information() {
    console.log("查找车辆数据")
    $.ajax({
        url: '/Freight/Freight_management',
        type: 'GET',
        data: { "state": 1, "id": $("#Fm_cydw_1").val() },
        success: function (data) {
            createRow(data)
            
        },
        error: function () {
            alert('Request Error');
        }
    })
}

function get_Vehicle_information_type(id_2) {
    console.log("查找对对应车型的车辆数据")
    $.ajax({
        url: '/Freight/Freight_management',
        type: 'GET',
        data: { "state": 4, "id": $("#Fm_cydw_1").val(), "id_2": id_2 },
        success: function (data) {
            createRow(data)

        },
        error: function () {
            alert('Request Error');
        }
    })
}
//显示车辆信息
function createRow(data) {
    $('#E_tb_e1 tr').remove();
    var index = 1
    $.each(data, function (i, item) {
        var $tr = $(`
                                                <tr style="height:20px;line-height:20px">
                                                    <td style="display:none">${item.cl_id}</td>
                                                    <td>${index}</td>
                                                    <td>${item.name}</td>
                                                    <td>${item.cl_cp}</td>
                                                    <td>${item.cl_zz}</td>
                                                    <td>${item.cl_rj}</td>
                                                    <td>${item.cl_dw}</td>
                                                </tr>
                                            `);
        $('#E_tb_e1').append($tr);
        index++
    })
}
//获取车型信息
function get_Vehicle_type() {
    console.log("查找车型数据")
    $.ajax({
        url: '/Freight/Freight_management',
        type: 'GET',
        data: { "state": 3, "id": $("#Fm_cydw_1").val() },
        success: function (data) {
            createRow_VT(data)
        },
        error: function () {
            alert('Request Error');
        }
    })
}
function createRow_VT(data) {
    $('#chexing tr').remove();
    var index = 1
    $.each(data, function (i, item) {
        var $tr = $(`
                                                <tr style="height:20px;line-height:20px">
                                                    <td>${index}</td>
                                                    <td>${item.cx_name}</td>
                                                    <td style="display:none">${item.cx_id}</td>
                                                    <td><a href="#" id="ckxx">查看详细</a></td>
                                                </tr>
                                            `);
        $('#chexing').append($tr);
        index++
    })
}
//查找车型对应阶梯价
function get_JTJ(e) {
    console.log("查找车型对应阶梯价")
    $.ajax({
        url: '/Freight/Freight_management',
        type: 'GET',
        data: { "state": 2, "id": e },
        success: function (data) {
            createRow_JTJ(data)
        },
        error: function () {
            alert('Request Error');
        }
    })
}

function createRow_JTJ(data) {
    $("#jt_tbody tr").remove()
    $("#JT_qbjt").val("")
    $("#JT_qbj").val("")
    $("#JT_zgjt").val("")
    $("#JT_zgj").val("")
    $.each(data, function (i, item) {
        if (item.jw_bian == "起步") {
            $("#JT_qbjt").val(item.jw_jt)
            $("#JT_qbj").val(item.jw_jg)
        }
        if (item.jw_bian == "最高") {
            $("#JT_zgjt").val(item.jw_jt)
            $("#JT_zgj").val(item.jw_jg)
        }
        if (item.jw_bian == "阶梯") {
            var $tr =
                $(`
                    <tr>
                        <td><input type="number" value="${item.jw_jt}" style="width:60px" class="JT_jtw"/></td>
                        <td style="width:60px">公里以下</td>
                        <td><input type="number" value="${item.jw_jg}" style="width:60px" class="JT_jtj"/></th>
                        <td style="width:60px">元/公里</th>
                        <td style="width:60px" ><a id="shc_jtw">删除阶梯</a></th>
                    </tr>
                `)
            $("#jt_tbody").append($tr)
        }
    })
}
function add_SHYF() {
    var index=0
    var sh_1 = []
    var sh_2 = []
    var sh_3 = []
    sh_1.push({
        ss_bian: "起步",
        ss_jt: $("#JT_qbjt").val(),
        ss_jg: $("#JT_qbj").val(),
        ss_cx: $("#cx_id").text(),
        ss_ss: $("#Fm_cydw_1").val(),
        ss_sh:"审核"
    })

    $("#jt_tbody tr").each(function (i, item) {
        sh_2.push({
            ss_bian: "阶梯",
            ss_jt: $("input[class=JT_jtw]",item).val(),
            ss_jg: $("input[class=JT_jtj]",item).val(),
            ss_cx: $("#cx_id").text(),
            ss_ss: $("#Fm_cydw_1").val(),
            ss_sh: "审核"
        })
        console.log(sh_2)
    })
    
    sh_3.push({
        ss_bian: "最高",
        ss_jt: $("#JT_zgjt").val(),
        ss_jg: $("#JT_zgj").val(),
        ss_cx: $("#cx_id").text(),
        ss_ss: $("#Fm_cydw_1").val(),
        ss_sh: "审核"
    })

    $.ajax({
        url: "/Freight/add_shyf",
        type: 'POST',
        data: JSON.stringify(sh_1),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (text) {
            index++
            $.ajax({
                url: "/Freight/add_shyf",
                type: 'POST',
                data: JSON.stringify(sh_2),
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (text) {
                    index++
                    $.ajax({
                        url: "/Freight/add_shyf",
                        type: 'POST',
                        data: JSON.stringify(sh_3),
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (text) {
                            index++
                            var sh_4 = []
                            sh_4.push({
                                sh_ren: "无",
                                sh_zt: "待审核",
                                sh_lx: "承运商报价审核",
                                sh_zhuantiao: $("#Fm_cydw_1").val(),
                            })
                            $.ajax({
                                url: "/Freight/add_shzt",
                                type: 'POST',
                                data: JSON.stringify(sh_4),
                                contentType: "application/json;charset=utf-8",
                                dataType: "json",
                                success: function (text) {
                                    alert("阶梯价提交审核")
                                },
                                error: function (text) {
                                    alert("系统错误" + text);
                                }
                            })
                        },
                        error: function (text) {
                            alert("系统错误" + text);
                        }
                    })
                },
                error: function (text) {
                    alert("系统错误" + text);
                }
            })
        },
        error: function (text) {
            alert("系统错误" + text);
        }
    })
}

//阶梯价提交审核
$(document).on("click", "#change_jtj", function () {
    add_SHYF()
})
//删除阶梯位
$(document).on("click", "#shc_jtw", function () {
    $("#shc_jtw").parent().parent().remove()
})

//点击修改方法
$(document).on("click", "#add_cys", function () {
    $("#show_xuigai").css({"display":"block"})
})
//点击关闭修改方法
$(document).on("click", ".XG_quxiao", function () {
    $("#show_xuigai").css({ "display": "none" })
})
//点击搜索方法
$(document).on("click", "#find_CL", function () {
    get_Vehicle_information()
})
//打开新增车辆
$(document).on("click", "#add_cl", function () {
    console.log($("#cx_id").text())
    if ($("#cx_id").text() == "") {
        alert("请先选择车型")
       
    }
    else {
        $("#add_clsj").css({ "display": "block" })
    }
    
})
//关闭新增车辆
$(document).on("click", ".QUED_clsj", function () {
    $("#add_clsj").css({ "display": "none" })
    gozero()
})


$(document).on("click", "#shangc_xx", function () {
    add_chel();
})
function add_chel() {
    var BOMinise=[]
    BOMinise.push({
        cl_cp: $("#add_cph_a1").val(),//
        cl_cc: $("#add_cc_a1").val(),//
        cl_ck: $("#add_ck_a1").val(),//
        cl_dw: $("#add_dw_a1").val(),//
        cl_zz: $("#add_zz_a1").val(),//
        cl_rj: $("#add_rj_a1").val(), //
        cl_ss: $("#Fm_cydw_1").val(), //
        cl_cx: $("#cx_id").text(), //
        //预留添加标识
    })
    console.log("共" + BOMinise.length + "数据");
    console.log(BOMinise);
    $.ajax({
        url: "/Freight/add_clsj",
        type: "POST",
        data: JSON.stringify(BOMinise),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (text) {
            if (text == 1) {
                get_Vehicle_information()
                get_Vehicle_type()
                gozero()
            }
            
        },
        error: function (text) {
            alert("系统错误" + text);
        }
    })
}
function gozero() {
    $("#add_cph_a1").val("")
    $("#add_cc_a1").val("")
    $("#add_ck_a1").val("")
    $("#add_dw_a1").val("")
    $("#add_zz_a1").val("")
    $("#add_rj_a1").val("")
}